/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zwu.t1;

import java.io.File;
import javax.jcr.*;
import javax.jcr.nodetype.*;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.Row;
import javax.jcr.query.RowIterator;
import org.apache.jackrabbit.core.TransientRepository;

/**
 *
 * @author Administrator
 */
public class JoinTwo {
public static void main(String[] args) throws Exception {

        System.out.println(new File(".").getAbsolutePath());
        Repository repository = new TransientRepository(new File("../data/t2"));
        Session session = repository.login(
                new SimpleCredentials("username", "password".toCharArray()));
        try {
            Node root = session.getRootNode();

           testjoin(session.getWorkspace());

        } finally {
            session.logout();
        }
    }


    

     public static void testjoin (Workspace wk) throws Exception
    {
        NodeTypeManager ntm = wk.getNodeTypeManager();
        for (int i=0; i<=1; i++)
        {
        NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
        //ntt.setMixin(true);
        String nameType = "type" + i;
        ntt.setName(nameType);
        ntt.setDeclaredSuperTypeNames(new String[]{"nt:unstructured"});
        //ntt.setPrimaryItemName("nt:unstructured");
        ntm.registerNodeType(ntt, true);
        System.out.println(ntm.hasNodeType(nameType));
        //Node node = wk.getNamespaceRegistry().registerNamespace(, null)
        Node n = wk.getSession().getRootNode();

        n = n.addNode("testing", nameType);

        n.setProperty("title", "doctor");
        n.setProperty("p", "p" + i);
        }
        wk.getSession().save();
          QueryManager qm = wk.getQueryManager();
            //String query = "select excerpt(.) from nt:resource where contains(., 'us')";
            //String query = "select * from nt:resource where contains(*, 'us')";
            String query = "select * from [type0] as t1 inner join [type1] as t2 on t1.title = t2.title";
            Query q = qm.createQuery(query, Query.JCR_SQL2);
            QueryResult result = q.execute();
            for (RowIterator it = result.getRows(); it.hasNext();) {
                Row r = it.nextRow();

                Node nn = r.getNode("t1");
                PropertyIterator pi2 = nn.getProperties();
                while (pi2.hasNext()) {
                    System.out.println(pi2.nextProperty().getName());
                }
                for (int i = 0; i < r.getValues().length; i++) {
                    System.out.println(r.getValues()[i].getString());
                }
                //Value excerpt = r.getNode("file").g("rep:excerpt(file)");
                //System.out.print(excerpt.getString());
            }



    }
}
